package com.components.cachewebview

import com.tencent.smtt.export.external.interfaces.WebResourceRequest
import com.tencent.smtt.export.external.interfaces.WebResourceResponse
import com.tencent.smtt.sdk.WebView
import java.io.File
import java.io.InputStream

class WebViewCacheInterceptorInst : WebViewRequestInterceptor {

    private var mInterceptor: WebViewRequestInterceptor? = null

    companion object {
        @Volatile
        private var webViewCacheInterceptorInst: WebViewCacheInterceptorInst? = null

        fun getInstance(): WebViewCacheInterceptorInst? {
            if (webViewCacheInterceptorInst == null) {
                synchronized(WebViewCacheInterceptorInst::class.java) {
                    if (webViewCacheInterceptorInst == null) {
                        webViewCacheInterceptorInst = WebViewCacheInterceptorInst()
                    }
                }
            }
            return webViewCacheInterceptorInst
        }
    }

    fun init(builder: WebViewCacheInterceptor.Companion.Builder?) {
        mInterceptor = builder?.build()
    }

    override fun interceptRequest(request: WebResourceRequest?): WebResourceResponse? {
        return mInterceptor?.interceptRequest(request)
    }

    override fun interceptRequest(url: String): WebResourceResponse? {
        return mInterceptor?.interceptRequest(url)
    }

    override fun getCachePath(): File? {
        return mInterceptor?.getCachePath()
    }

    override fun clearCache() {
        mInterceptor?.clearCache()
    }

    override fun enableForce(force: Boolean) {
        mInterceptor?.enableForce(force)
    }

    override fun getCacheFile(url: String): InputStream? {
        return mInterceptor?.getCacheFile(url)
    }

    override fun initAssetsData() {
        AssetsLoader.getInstance()?.initData()
    }

    override fun loadUrl(webView: WebView?, url: String) {
        mInterceptor?.loadUrl(webView, url)
    }

    override fun loadUrl(url: String, userAgent: String) {
        mInterceptor?.loadUrl(url, userAgent)
    }

    override fun loadUrl(
        url: String,
        additionalHttpHeaders: Map<String?, String?>?,
        userAgent: String
    ) {
        mInterceptor?.loadUrl(url, additionalHttpHeaders, userAgent)
    }

    override fun loadUrl(
        webView: WebView?,
        url: String,
        additionalHttpHeaders: Map<String, String>
    ) {
        mInterceptor?.loadUrl(webView, url, additionalHttpHeaders)
    }


}